//
//  MovieViewController.m
//  豆瓣zlw
//
//  Created by lanou3g on 16/1/6.
//  Copyright © 2016年 zlw. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieModel.h"
#import "MovieTableView.h"
#import <UIImageView+WebCache.h>
@interface MovieViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *arrAllData;
}

@end

@implementation MovieViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.backgroundColor=[UIColor orangeColor];
    //遵守协议
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self getDatas];
}
//获取数据
-(void)getDatas
{
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:MOVIEURL]];
    NSURLSession *session=[NSURLSession sharedSession];
    NSURLSessionTask *task=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data)
        {
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            arrAllData=[NSMutableArray array];
            for (NSDictionary *dic in dict[@"result"])
            {
                MovieModel *model=[[MovieModel alloc] init];
                [model setValuesForKeysWithDictionary:dic[@"result"]];
                [model setValuesForKeysWithDictionary:dic];
                [arrAllData addObject:model];
            }
        }
        [self performSelectorOnMainThread:@selector(mainAction) withObject:nil waitUntilDone:nil];
       
    }];
    [task resume];
    
}
-(void)mainAction
{
    [self.HUD hide:YES];
    [self.tableView reloadData];
    
}
//代理必须实现的方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arrAllData.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieTableView *cell=[tableView dequeueReusableCellWithIdentifier:@"moive_id" forIndexPath:indexPath];
    MovieModel *model=arrAllData[indexPath.row];
    cell.lable.text=model.movieName;
    [cell.imV sd_setImageWithURL:[NSURL URLWithString:model.pic_url]];
    return cell;
}

#pragma mark------转到详情页面
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    //取到目标控制器
    id  des=segue.destinationViewController;
    //根据sender找到从哪里跳过去
    MovieTableView *cell=(MovieTableView *)sender;
    NSIndexPath *path=[self.tableView indexPathForCell:cell];
    //用KVC给目标控制器的属性赋值,把值传过去
    [des setValue:arrAllData[path.row] forKey:@"getmodel"];
}















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
